<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        function order500(orderType, isPay, count) {
            if (orderType === 1 && isPay === true) {
                console.log('恭喜中奖100 优惠券')
            } else {
                return 'nextSuccessor'
            }
        }

        function order200(orderType, isPay, count) {
            if (orderType === 2 && isPay === true) {
                console.log('恭喜中奖20 优惠券')
            } else {
                return 'nextSuccessor'
            }
        }


        function orderNormal(orderType, isPay, count) {
            if (count > 0) {
                console.log('恭喜中奖10 优惠券')
            } else {
                console.log('很遗憾没有优惠券')
            }
        }




        var Chain = function fn(fn) {
            this.fn = fn
            this.successor = null
        }

        Chain.prototype.setNextSuccessor = function (successor) {
            return (this.successor = successor)
        }

        Chain.prototype.passRequest = function () {
            var res = this.fn.apply(this, arguments)
            if (res === 'nextSuccessor') {
                return this.successor && this.successor.passRequest.apply(this.successor, arguments)
            }
            return res
        }

        var chainOrder500 = new Chain(order500)
        var chainOrder200 = new Chain(order200)
        var chainOrderNormal = new Chain(orderNormal)

        chainOrder500.setNextSuccessor(chainOrder200)
        chainOrder200.setNextSuccessor(chainOrderNormal)


        chainOrder500.passRequest(1, false, 10)
        // chainOrder500.passRequest(2, true, 500)
        // console.log(chainOrder500, chainOrder200, chainOrderNormal)
    </script>
</body>

</html>